<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/redirects.sjs">
<head>
<title>Entries redirection handling during update test</title>

<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script class="testbody" type="text/javascript">

var gCurrentManifestVersion = 1;

function manifestCached()
{
  var entries = [
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/dynamicRedirect.sjs", false],
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/explicitRedirect.sjs", true],
  ];
  OfflineTest.checkCacheEntries(entries, manifestCachedContinue);
}

function manifestCachedContinue()
{
  OfflineTest.is(gCurrentManifestVersion, 1, "Cached event for manifest version one");

  // Now add one dynamic entry (now with content overriden redirect sjs)
  applicationCache.mozAdd(
    "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/dynamicRedirect.sjs");

  // Wait for the dynamic entry be added to the cache...
  OfflineTest.waitForAdd(
    "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/dynamicRedirect.sjs",
    function() {
      // ...check it is there...
      OfflineTest.checkCache(
        "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/dynamicRedirect.sjs",
        true,
        function() {
          // ...revert state of the dynamic entry on the server, now we get the redirect...
          OfflineTest.setSJSState(
            "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/dynamicRedirect.sjs",
            "");

          // ...update manifest to the new version on the server...
          OfflineTest.setSJSState(
            "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/redirects.sjs",
            "second");
          gCurrentManifestVersion = 2;

          // ...and finally invoke the cache update.
          applicationCache.update();
        });
    });
}

function manifestUpdated()
{
  switch (gCurrentManifestVersion)
  {
    case 2:
      // Check the dynamic entry was removed from the cache (because of the redirect)...
      OfflineTest.checkCache(
        "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/dynamicRedirect.sjs",
        false,
        function () {
          // ...return back redirect for the explicit entry...
          OfflineTest.setSJSState(
            "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/explicitRedirect.sjs",
            "");

          // ...update the manifest to the third version...
          OfflineTest.setSJSState(
            "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/redirects.sjs",
            "third");
          gCurrentManifestVersion = 3;

          // ...and invoke the cache update, now we must get error.
          applicationCache.update();
        });

      break;

    case 3:
      OfflineTest.ok(false, "Update didn't fail for third version of the manifest");
      finish();
      break;
  }
}

function manifestError()
{
  switch (gCurrentManifestVersion)
  {
    case 1:
      OfflineTest.ok(false, "Error not expected when caching the first version of the manifest");
      finish();
      break;
    case 2:
      OfflineTest.ok(false, "Error not expected when updating to second version of the manifest");
      finish();
      break;
    case 3:
      OfflineTest.ok(true, "Error expected when updating to third version of the manifest");
      finish();
      break;
  }
}

function finish()
{
  OfflineTest.teardownAndFinish();
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

if (OfflineTest.setup()) {
  applicationCache.onerror = OfflineTest.priv(manifestError);
  applicationCache.onupdateready = OfflineTest.priv(manifestUpdated);
  applicationCache.oncached = OfflineTest.priv(manifestCached);

  // Override sjs redirects on the server, it will now return 200 OK and the content
  OfflineTest.setSJSState(
      "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/explicitRedirect.sjs",
      "on");
  OfflineTest.setSJSState(
      "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/dynamicRedirect.sjs",
      "on");
}

</script>

</head>

<body>
</body>
</html>
